package com.example.design.patterns.geekbang.创建型.单例模式.explain.懒汉模式;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 懒汉模式 性能和并发度低
 */
public class IdGenerator {
    private static IdGenerator instance;
    private AtomicLong id = new AtomicLong(0);

    private IdGenerator() {
    }

    public static synchronized IdGenerator getInstance() {
        if (instance == null) {
            instance = new IdGenerator();
        }
        return instance;
    }

    public long getId() {
        return id.incrementAndGet();
    }
}
